﻿@{
    var treeList = Html.DevExpress().TreeList(settings =>
    {
        settings.Name = "TreeListMainMenu";
        settings.CallbackRouteValues = new { Controller = "MainMenu", Action = "TreeListMainMenuPartial" };

        settings.SettingsEditing.AddNewNodeRouteValues = new { Controller = "MainMenu", Action = "TreeListMainMenuPartialAddNew" };
        settings.SettingsEditing.UpdateNodeRouteValues = new { Controller = "MainMenu", Action = "TreeListMainMenuPartialUpdate" };
        settings.SettingsEditing.DeleteNodeRouteValues = new { Controller = "MainMenu", Action = "TreeListMainMenuPartialDelete" };
        settings.SettingsEditing.NodeDragDropRouteValues = new { Controller = "MainMenu", Action = "TreeListMainMenuPartialMove" };
        settings.SettingsEditing.Mode = TreeListEditMode.EditFormAndDisplayNode;
        settings.CommandColumn.ButtonType = System.Web.UI.WebControls.ButtonType.Image;
        settings.CommandColumn.SetColVisibleIndex(14);
        settings.CommandColumn.SetHeaderCaptionTemplateContent("Tùy chọn");
        settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);
        settings.Settings.HorizontalScrollBarMode = ScrollBarMode.Auto;
        settings.Settings.VerticalScrollBarMode = ScrollBarMode.Hidden;

        settings.CommandColumn.NewButton.Visible = true;
        settings.CommandColumn.NewButton.Image.Url = @Url.Content("~/Content/img/icon-24-new.png");
        settings.CommandColumn.NewButton.Image.ToolTip = "Thêm mới";
        settings.CommandColumn.NewButton.Image.AlternateText = "Thêm";

        settings.CommandColumn.EditButton.Visible = true;
        settings.CommandColumn.EditButton.Image.Url = @Url.Content("~/Content/img/icon-24-edit.png");
        settings.CommandColumn.EditButton.Image.ToolTip = "Thay đổi";
        settings.CommandColumn.EditButton.Image.AlternateText = "Sửa";

        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.DeleteButton.Image.Url = @Url.Content("~/Content/img/icon-24-delete.png");
        settings.CommandColumn.DeleteButton.Image.ToolTip = "Xóa";
        settings.CommandColumn.DeleteButton.Image.AlternateText = "Xóa";

        settings.CommandColumn.UpdateButton.Visible = true;
        settings.CommandColumn.UpdateButton.Image.Url = @Url.Content("~/Content/img/icon-24-ok.png");
        settings.CommandColumn.UpdateButton.Image.ToolTip = "Lưu lại";
        settings.CommandColumn.UpdateButton.Image.AlternateText = "Lưu";

        settings.CommandColumn.CancelButton.Visible = true;
        settings.CommandColumn.CancelButton.Image.Url = @Url.Content("~/Content/img/icon-24-cancel.png");
        settings.CommandColumn.CancelButton.Image.ToolTip = "Hủy";
        settings.CommandColumn.CancelButton.Image.AlternateText = "Hủy";

        settings.CommandColumn.Visible = true;



        settings.AutoGenerateColumns = false;

        settings.KeyFieldName = "menuId";
        settings.ParentFieldName = "parentId";
        settings.InitNewNode = (s, e) =>
        {
            e.NewValues["parentId"] = 0;
            e.NewValues["isActive"] = true;
            e.NewValues["ordering"] = 1;
        };

        settings.Columns.Add(
        column =>
        {
            column.FieldName = "menuId";
            column.Caption = "ID";
            column.Visible = false;

        });
        
        settings.Columns.Add(
        column =>
        {
            column.FieldName = "menuName";
            column.Caption = "Tên menu";
            column.Index = 2;
            column.Width = System.Web.UI.WebControls.Unit.Percentage(100);
        });
        settings.Columns.Add(
        column =>
        {
            column.FieldName = "menuController";
            column.Caption = "Controller";
            column.Index = 3;
            column.Width = 100;
        });
        settings.Columns.Add(
        column =>
        {
            column.FieldName = "menuAction";
            column.Caption = "Action";
            column.Index = 4;
            column.Width = 100;
        });
        settings.Columns.Add(
        column =>
        {
            column.FieldName = "parentId";
            column.Caption = "Menu Cha";
            column.Visible = false;
            column.ColumnType = MVCxTreeListColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            
            //comboBoxProperties.ConvertEmptyStringToNull = false;
            comboBoxProperties.ValueField = "parentId";
            //comboBoxProperties.NullDisplayText = "Không có menu cha";
            comboBoxProperties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;
            comboBoxProperties.Items.Add("Không có menu cha", 0);
            foreach (var value in Model)
            {
                comboBoxProperties.Items.Add(value.menuName, value.menuId);
            }
            comboBoxProperties.ValueType = typeof(long);
            column.ReadOnly = false;
            column.Index = 5;
            column.Width = 200;
        });
        settings.Columns.Add(
        column =>
        {
            column.FieldName = "menuSuffix";
            column.Caption = "Icon";
            column.ColumnType = MVCxTreeListColumnType.TextBox;
            column.Visible = true;
            column.Index = 1;
            column.Width = 60;
            column.SetDataCellTemplateContent(c =>
            {
                string iconname = DataBinder.Eval(c.DataItem, "MenuSuffix").ToString();
                ViewContext.Writer.Write("<i style=\"font-size:20px;\" class=\""+ iconname +"\"></i>");
            });
        });
        settings.Columns.Add(
        column =>
        {
            column.Visible = false;
            column.FieldName = "menuParams";
            column.Caption = "Prameter";
            column.Index = 7;
            column.Width = 200;
        });
        settings.Columns.Add(
    column =>
    {
        column.FieldName = "isActive";
        column.Caption = "Hiển thị";
        column.Index = 9;
        column.Width = 80;
        column.ColumnType = MVCxTreeListColumnType.CheckBox;
        column.SetDataCellTemplateContent(c =>
        {
            bool actived = (bool)DataBinder.Eval(c.DataItem, "isActive");
            object menu_id_obj = DataBinder.Eval(c.DataItem, "menuId");
            string updateLink = Url.Action("ToogleActive", "Base", new { keyvalue = menu_id_obj, active = !actived, tablename = "tblMainMenu", keyname = "menuId" });
            ViewContext.Writer.Write(
                "<a href=\"" + updateLink + "\" id=\"id_button_active_" + menu_id_obj + "\" class=\"button btn-active\"><i class=\"font-icon-" + (actived ? "check" : "check-empty") + "\"></i></a>"
            );
        });
    });
        settings.Columns.Add(
    column =>
    {
        column.FieldName = "ordering";
        column.Caption = "Vị trí";
        column.ColumnType = MVCxTreeListColumnType.SpinEdit;
        column.Index = 8;
        column.SetDataCellTemplateContent(c =>
        {
            int ordering = DataBinder.Eval(c.DataItem, "ordering") == DBNull.Value ? 0 : Convert.ToInt32(DataBinder.Eval(c.DataItem, "ordering"));
            object menu_id_obj = DataBinder.Eval(c.DataItem, "menuId");
            string url = Url.Action("Reorder", "Base", new { keyvalue = menu_id_obj,tablename = "tblMainMenu", keyname = "menuId" });
            string strurl = Url.Action("CustomReorder", "Base", new { keyvalue = menu_id_obj, tablename = "tblMainMenu", keyname = "menuId"});

            string input = "<input id=\"ordering_value_" + menu_id_obj + "\" class=\"ordering_value\" value=\"" + ordering + "\" onchange=\"changeOrder('" + strurl + "',this.value)\" />";
            string btnDown = "<a class=\"btn-change-order font-icon-caret-down\" href=\"" + url + "&ordertype=down\"></a>";
            string btnUp = "<a class=\"btn-change-order font-icon-caret-up\"  href=\"" + url + "&ordertype=up\"></a>";
            ViewContext.Writer.Write(btnDown + input + btnUp);
        });
    });

        settings.SettingsPager.Visible = true;
        settings.SettingsSelection.Enabled = true;
    });
    if (ViewData["EditError"] != null)
    {
        treeList.SetEditErrorText((string)ViewData["EditError"]);
    }
}
@treeList.Bind(Model).GetHtml()